package com.qing.springcloud.dao;

import com.qing.springcloud.common.GroupMemberRoleEnum;
import com.qing.springcloud.dto.group.GroupMemberDTO;
import lombok.extern.slf4j.Slf4j;
import com.qing.springcloud.entity.GroupMember;
import com.qing.springcloud.mapper.GroupMemberMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * (group_member)数据DAO
 *
 * @author kancy
 * @since 2025-07-09 19:32:19
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class GroupMemberDao extends ServiceImpl<GroupMemberMapper, GroupMember> {

    public boolean existsByGroupIdAndUserId(Long groupId, Long userId) {
        return lambdaQuery()
                .eq(GroupMember::getGroupId, groupId)
                .eq(GroupMember::getUserId, userId)
                .exists();
    }

    public List<Long> findAdminIdsByGroupId(Long groupId) {
        return lambdaQuery()
               .eq(GroupMember::getGroupId, groupId)
                .in(GroupMember::getRole, GroupMemberRoleEnum.ADMIN.getCode(), GroupMemberRoleEnum.OWNER.getCode())
               .list()
               .stream()
               .map(GroupMember::getUserId)
               .collect(Collectors.toList());
    }

    public Optional<GroupMember> findByGroupIdAndUserId(Long groupId, Long operatorId) {
        return Optional.ofNullable(lambdaQuery()
                                .eq(GroupMember::getGroupId, groupId)
                                .eq(GroupMember::getUserId, operatorId)
                                .one());
    }

    public Optional<GroupMember> findActiveAdminInGroup(Long groupId, Long oldOwnerId) {

        return Optional.ofNullable(lambdaQuery()
                               .eq(GroupMember::getGroupId, groupId)
                               .eq(GroupMember::getRole, GroupMemberRoleEnum.ADMIN.getCode())
                               .ne(GroupMember::getUserId, oldOwnerId)
                               .one());
    }

    public Optional<GroupMember> findActiveNormalMemberInGroup(Long groupId, Long oldOwnerId) {
        return Optional.ofNullable(lambdaQuery()
                              .eq(GroupMember::getGroupId, groupId)
                              .eq(GroupMember::getRole, GroupMemberRoleEnum.MEMBER.getCode())
                              .ne(GroupMember::getUserId, oldOwnerId)
                              .one());
    }

    public List<GroupMember> findByGroupId(Long groupId,Integer status) {
        return lambdaQuery().eq(GroupMember::getGroupId, groupId).eq(GroupMember::getStatus, status).list();
    }

    public boolean existsActiveMember(Long groupId, Long senderId) {
        return lambdaQuery()
               .eq(GroupMember::getGroupId, groupId)
               .eq(GroupMember::getUserId, senderId)
               .eq(GroupMember::getIsDeleted, 0)
               .exists();
    }

    public List<Long> findActiveMemberIds(Long groupId) {
        return lambdaQuery()
              .eq(GroupMember::getGroupId, groupId)
              .eq(GroupMember::getIsDeleted, 0)
              .list()
              .stream()
              .map(GroupMember::getUserId)
              .collect(Collectors.toList());
    }
}